package mylab.starters.mybatis.config;

import lombok.RequiredArgsConstructor;
import mylab.starters.mybatis.plugins.*;
import mylab.starters.mybatis.support.foreignKey.ForeignKeyService;
import mylab.starters.mybatis.support.foreignKey.impl.ForeignKeyServiceImpl;
import mylab.starters.mybatis.support.tenant.TenantColumnModeInterceptor;
import mylab.starters.mybatis.support.tenant.TenantSchemaModeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@RequiredArgsConstructor(onConstructor_ = @Autowired)
@EnableConfigurationProperties({DbProperties.class})
@Configuration
public class DbAutoConfiguration {

    private final DbProperties properties;

    @Bean
    public MyPaginationInnerInterceptor paginationInnerInterceptor() {
        return new MyPaginationInnerInterceptor(properties);
    }

    /**
     * 该插件只支持 MySQL5.6.3 以上版本
     * 该插件只建议在开发环境中使用，不建议在生产环境使用
     */
    @Bean
    @ConditionalOnProperty(value = DbProperties.PREFIX + ".enable-optimistic-locker", havingValue = "true", matchIfMissing = true)
    public MyBlockAttackInnerInterceptor blockAttackInnerInterceptor() {
        return new MyBlockAttackInnerInterceptor();
    }

    @Bean
    @ConditionalOnProperty(value = DbProperties.PREFIX + ".enableDataChangeRecorder", havingValue = "true", matchIfMissing = true)
    public MyDataChangeRecorderInnerInterceptor dataChangeRecorderInnerInterceptor() {
        return new MyDataChangeRecorderInnerInterceptor();
    }

    @Bean
    @ConditionalOnProperty(value = DbProperties.PREFIX + ".enableDataPermission", havingValue = "true", matchIfMissing = true)
    public MyDataPermissionInterceptor dataPermissionInterceptor() {
        return new MyDataPermissionInterceptor();
    }

    @Bean
    @ConditionalOnProperty(value = DbProperties.PREFIX + ".enableIllegalSql", havingValue = "true", matchIfMissing = true)
    public MyIllegalSQLInnerInterceptor illegalSQLInnerInterceptor() {
        return new MyIllegalSQLInnerInterceptor();
    }

    @Bean
    @ConditionalOnProperty(value = DbProperties.PREFIX + ".enableOptimisticLocker", havingValue = "true", matchIfMissing = true)
    public MyOptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
        return new MyOptimisticLockerInnerInterceptor();
    }


    @Bean
    @ConditionalOnProperty(value = DbProperties.PREFIX + ".dynamicTable.enable", havingValue = "true", matchIfMissing = true)
    public MyDynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor() {
        return new MyDynamicTableNameInnerInterceptor(properties.getDynamicTable());
    }

    @Bean
    @ConditionalOnProperty(value = DbProperties.PREFIX + ".foreignKey.enable", havingValue = "true")
    public MyForeignKeyInterceptor foreignKeyInterceptor() {
        ForeignKeyService foreignKeyService = new ForeignKeyServiceImpl(properties.getForeignKey().getEntityPackages());
        return new MyForeignKeyInterceptor(foreignKeyService);
    }

    @Bean
    @ConditionalOnProperty(value = DbProperties.PREFIX + ".tenant.mode", havingValue = "COLUMN")
    public TenantColumnModeInterceptor tenantColumnModeInterceptor() {
        return new TenantColumnModeInterceptor(properties.getTenant().getColumn());
    }

    @Bean
    @ConditionalOnProperty(value = DbProperties.PREFIX + ".tenant.mode", havingValue = "SCHEMA")
    public TenantSchemaModeInterceptor tenantSchemaModeInterceptor() {
        return new TenantSchemaModeInterceptor(properties.getTenant().getSchema());
    }


}
